package cd.tools_test;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;

import cd.serialport.Serialport;
import cd.tools.Tools;

public class SerialportActivity extends AppCompatActivity {

    private Serialport mSerialport;
    private int fd;
    private TextView tvOutput;
    private EditText etInput;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_serialport);

        tvOutput = (TextView) findViewById(R.id.tvOutput);
        tvOutput.setMovementMethod(ScrollingMovementMethod.getInstance());
        etInput = (EditText) findViewById(R.id.etInput);

        Log.d("chenda","Tools.getVersion() :  " + Tools.getVersion());

        mSerialport = new Serialport();
        fd = mSerialport.open_serialport("dev/ttyS1",115200,0);
//        byte[] data = new byte[]{0x01 ,0x01 ,0x01 ,0x01 ,0x01 ,0x12 ,(byte)0xDB ,0x01 ,0x04 ,0x03 ,(byte)0xF6};
//        mSerialport.write_serialport(fd,data,data.length);
//        Log.d("chenda","fd = " + fd);
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                final byte[] buff = new byte[512];
//                while(true){
//                    final int len = mSerialport.read_serialport(fd,buff,buff.length,500);
//                    Log.d("chenda","read : " + Hex.byteArray2Hex(buff,0,len));
//                    if (len <= 0)
//                        continue;
//                    runOnUiThread(new Runnable() {
//                        @Override
//                        public void run() {
//                            tvAppend(Hex.byteArray2Hex(buff,0,len));
//                        }
//                    });
//                }
//            }
//        }).start();

        byte[] buff = new byte[512];
        byte[] data;
        int len;

//        data = new byte[]{0x02, (byte)0xbf, 0x00, 0x00, 0x00, 0x00, 0x00};
//        Log.d("chenda","write : " + Hex.byteArray2Hex(data,0,data.length));
//        mSerialport.write_serialport(fd,data,data.length);
//        len = mSerialport.read_serialport(fd,buff,buff.length,5000);
//        Log.d("chenda","read : " + Hex.byteArray2Hex(buff,0,len));





//        data = new byte[]{0x02,(byte)0xb3,0x22,0x00,0x40,0x00,0x00,0x00,0x10,0x76,(byte)0xed,0x2f,(byte)0xc4,
//                (byte)0x84,0x58,(byte)0xca
//,(byte)0xb2,0x76,(byte)0xed,0x2f,(byte)0xc4,(byte)0x84,0x58,(byte)0xca,(byte)0xb2,0x00,0x00,0x00,0x00,0x00,0x00,0x00
//,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
//,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
//,0x00,0x3b,(byte)0x9a,0x6e,(byte)0xf4,(byte)0xb8,0x17};
//        Log.d("chenda","write : " + Hex.byteArray2Hex(data,0,data.length));
//        mSerialport.write_serialport(fd,data,data.length);
//        len = mSerialport.read_serialport(fd,buff,buff.length,5000);
//        Log.d("chenda","read : " + Hex.byteArray2Hex(buff,0,len));
//
////        try {
////            Thread.sleep(30*1000);
////        } catch (InterruptedException e) {
////            e.printStackTrace();
////        }
//
//        data = new byte[]{0x02,(byte)0xb3,0x22,0x00,0x40,0x00,0x00,0x01,0x10,(byte)0x95,0x60,(byte)0x90,0x2d,(byte)0xd8,0x39,0x28,
//                (byte)0xb0,(byte)0x95,0x60,(byte)0x90,0x2d,(byte)0xd8,0x39,0x28,(byte)0xb0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
//0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
//0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
//0x00,0x66,(byte)0x86,0x3d,0x5d,(byte)0xd3,0x25};
//        Log.d("chenda","write : " + Hex.byteArray2Hex(data,0,data.length));
//        mSerialport.write_serialport(fd,data,data.length);
//        len = mSerialport.read_serialport(fd,buff,buff.length,5000);
//        Log.d("chenda","read : " + Hex.byteArray2Hex(buff,0,len));
//
////        mSerialport.close_serialport(fd);
////        fd = mSerialport.open_serialport("dev/ttyS1",115200,0);
//
//        Log.d("chenda","write : " + Hex.byteArray2Hex(data,0,data.length));
//        mSerialport.write_serialport(fd,data,data.length);
//        len = mSerialport.read_serialport(fd,buff,buff.length,5000);
//        Log.d("chenda","read : " + Hex.byteArray2Hex(buff,0,len));
//
////        try {
////            Thread.sleep(30*1000);
////        } catch (InterruptedException e) {
////            e.printStackTrace();
////        }
//
//        Log.d("chenda","write : " + Hex.byteArray2Hex(data,0,data.length));
//        mSerialport.write_serialport(fd,data,data.length);
//        len = mSerialport.read_serialport(fd,buff,buff.length,5000);
//        Log.d("chenda","read : " + Hex.byteArray2Hex(buff,0,len));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mSerialport.close_serialport(fd);
    }


    public void tvAppend(String data) {
        tvOutput.append(data);
        int offset = tvOutput.getLineCount() * tvOutput.getLineHeight();
        if (offset > tvOutput.getHeight()) {
            tvOutput.scrollTo(0, offset - tvOutput.getHeight());
        }
    }
}
